package com.iweb.orderApp;

import java.util.Scanner;

//订单系统服务类
public class OrderService {
    private MenuItems[] menu = new MenuItems[4];//菜单数据
    private OrderSet[] order = new OrderSet[5];

    public MenuItems[] getMenu() {
        return menu;
    }

    public void setMenu(MenuItems[] menu) {
        this.menu = menu;
    }

    public OrderSet[] getOrder() {
        return order;
    }

    public void setOrder(OrderSet[] order) {
        this.order = order;
    }

    public void initial() {
        MenuItems ftq = new MenuItems("佛跳墙", 40, 0);
        MenuItems shxd = new MenuItems("松花肚", 20, 0);
        MenuItems fqfp = new MenuItems("红烧肉", 30, 0);
        MenuItems bzj = new MenuItems("白斩鸡", 21, 0);
        menu = new MenuItems[]{ftq, shxd, fqfp, bzj};

        order = new OrderSet[5];

        order[0] = new OrderSet("张三", "佛跳墙", 2,
                18, "苏园路6号", 1, 80);
        order[1] = new OrderSet("李强", "红烧肉", 1,
                17, "宛平南6路", 0, 30);

    }

    //获取用户输入工具类对象
    private Scanner scanner = new Scanner(System.in);

    public void startMenu() {
        System.out.println("欢迎使用【饿了么】，订餐系统：");
        boolean goon = true;
        do {
            System.out.println("********************************");
            System.out.println("1.我要订餐");
            System.out.println("2.查看餐袋");
            System.out.println("3.签收订单");
            System.out.println("4.删除订单");
            System.out.println("5.我要点赞");
            System.out.println("6.退出系统");
            System.out.println("请选择：");

            if (scanner.hasNextInt()) {
                int option = scanner.nextInt();

                switch (option) {
                    case 1:
                        myOrdering();
                        break;
                    case 2:
                        showOrderingSets();
                        break;
                    case 3:
                        checkoff();
                        break;
                    case 4:
                        delSet();
                        break;
                    case 5:
                        goodSet();
                        break;
                    case 6:
                        goon = false;
                        break;
                    default:
                        break;
                }
            } else {
                System.out.println("有误，重新输入：");
                scanner.next();
            }
        } while (goon);
        System.out.println("谢谢使用,886");
    }

    private void myOrdering() {
        int index = -1;
        for (int i = 0; i < order.length; i++) {
            if (order[i] == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("别点了");
        } else {

            System.out.println("******我要订餐********");
            System.out.println("请输入订餐人姓名：");
            String name = scanner.next();
            showDish();
            System.out.println("请输入您要的菜品编号:");
            int dishnum = scanner.nextInt();
            System.out.println("请输入您要的份数:");
            int dishcount = scanner.nextInt();
            System.out.println("请输入您预定送达的时间:");
            int disht = scanner.nextInt();
            System.out.println("请输入您的地址:");
            String addr = scanner.next();
            double dis = menu[dishnum - 1].getDishPrice() * dishcount;
            double money;
            double scf = 5;
            if (dis <= 50) {
                scf = 5;
                money = dis + scf;
            } else {
                scf = 0;
                money = dis;
            }
            OrderSet yrs = new OrderSet(name, menu[dishnum - 1].getDishName(),
                    dishcount, disht, addr, 0, money);

            order[index] = new OrderSet(name, menu[dishnum - 1].getDishName(),
                    dishcount, disht, addr, 0, money);

            System.out.println("订餐成功！");
            System.out.println("您的订餐菜品是：" + yrs.getDishname() + yrs.getDishNum() + "份。");
            System.out.println("送餐时间:" + disht + "时。");
            System.out.println("餐费:" + dis + "元，送餐费:" + scf + "元，总计:" + money + "元。");
        }
    }

    public void showOrderingSets() {
        //TODO
        System.out.println("以下为饿了么订单详细信息:");
        System.out.println("序号" + "      " + "订餐人" + "      " + "菜品" + "      " + "份数" + "      " + "地址" + "          " +
                "预约时间" + "      " + "总金额" + "        " + "状态");
        for (int i = 0; i < order.length; i++) {
            if (order[i] == null) {
                break;
            }
            String zt;
            if (order[i].getStatus() == 1) {
                zt = "已送达";
            } else {
                zt = "未签收";
            }
            System.out.printf(i + 1 + "             " + order[i].getUsrname() + "      " + order[i].getDishname() + "      " +
                    order[i].getDishNum() + "份" + "      " + order[i].getAddr() + "      " + order[i].getTime() + " 时     " +
                    order[i].getSumPrice() + "元" + "      " + zt + "\n");
        }
    }

    private void showDish() {
        System.out.println("序号       菜名       单价       点赞数 ");
        for (int i = 0; i < menu.length; i++) {
            System.out.printf(i + 1 + "       " + menu[i].getDishName() + "       "
                    + menu[i].getDishPrice() + "       " + menu[i].getGoodNum() + "\n");
        }
    }

    private void checkoff() {
        showOrderingSets();
        System.out.println("输入您要签收的订单序号：");
        int flag = -1;
        int onum = scanner.nextInt();
        for (int i = 0; i < order.length; i++) {
            if (order[i] != null) {
                if (onum == i + 1) {
                    flag = onum;
                    order[i].setStatus(1);
                    System.out.println("您已成功签收！");
                    showOrderingSets();
                }
            }
        }
        if (flag == -1) {
            System.out.println("输入不合法，请重新操作!");
        }
    }

    private void delSet() {
        showOrderingSets();
        System.out.println("输入您想删除的订单序号：");
        int flag1 = -1;
        int onum1 = scanner.nextInt();
        for (int i = 0; i < order.length; i++) {
            if (onum1 == i + 1) {
                if (order[i].getStatus() == 0) {
                    System.out.println("订单已预定但未签收，无法删除！");
                    break;
                }
                flag1 = i;
                for (int j = i + 1; j < order.length; j++) {
                    order[j - 1] = order[j];
                    if (order[j] == null) {
                        order[j - 1] = null;
                    }
                }
                break;
            }
            System.out.println("您已成功删除该订单：");
        }

        if (flag1 == -1) {
            System.out.println("删除不合法，请重新操作!");
        }
        showOrderingSets();
    }

    private void goodSet() {
        showDish();
        System.out.println("输入您想点赞的菜品：");
        int flag = -1;
        int onum = scanner.nextInt();
        for (int i = 0; i < menu.length; i++) {
            if (menu[i] != null) {
                if (onum == i + 1) {
                    flag = onum;
                    menu[i].setGoodNum(menu[i].getGoodNum() + 1);
                    System.out.println("您已成功点赞！");
                    showDish();
                }
            }
        }
        if (flag == -1) {
            System.out.println("输入不合法，请重新操作!");
        }
    }

}